package com.indsvr.system.task;

import com.indsvr.common.core.domain.entity.SysDept;
import com.indsvr.common.core.domain.entity.SysUser;
import com.indsvr.common.utils.ShiroUtils;
import com.indsvr.common.utils.sql.JdbcUtils;
import com.indsvr.system.service.ISysDeptService;
import com.indsvr.system.service.ISysPostService;
import com.indsvr.system.service.ISysRoleService;
import com.indsvr.system.service.ISysUserService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

@Component("userTask")
public class UserTask {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    public void sync() throws Exception {
        String url = "jdbc:mysql://192.168.2.113:3306/dgs4db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        String user = "dgs4user";
        String pwd = "Pcitc@dgs4";

        JdbcUtils connection = JdbcUtils.getConnection(url, user, pwd);

        List<UserEntity> select = connection.select(
                "SELECT c_userid,c_code,c_name,c_sex,c_idcard,c_part_code,c_part_oid FROM t_bsstaff",
                UserEntity.class);

        if (select.size() < 1) {
            return;
        }

        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("insert into sys_user (user_id, dept_id , login_name , user_name , user_type , sex , c_part_code , c_idcard) values ");
        for (int i = 0; i < select.size(); i++) {
            UserEntity userEntity = select.get(i);


            String c_userid = userEntity.c_userid;
            String c_part_oid = userEntity.c_part_oid;

            String c_code = userEntity.c_code;
            String c_name = userEntity.c_name;
            int sex = "男".equals(userEntity.c_sex) ? 0 : 1;
            String c_part_code = userEntity.c_part_code;
            String c_idcard = userEntity.c_idcard;


            stringBuffer.append("("+c_userid+","+c_part_oid+",'"+c_code+"','"+c_name+"','00',"+sex+",'"+c_part_code+"','"+c_idcard+"')");
            if (i == select.size() - 1) {
                stringBuffer.append(" ON DUPLICATE KEY UPDATE \n" +
                        "dept_id = VALUES(dept_id),\n" +
                        "login_name = VALUES(login_name),\n" +
                        "user_name = VALUES(user_name),\n" +
                        "user_type = VALUES(user_type),\n" +
                        "sex = VALUES(sex),\n" +
                        "c_part_code = VALUES(c_part_code),\n" +
                        "c_idcard =  VALUES(c_idcard)");
                break;
            }
            stringBuffer.append(",");
        }
        Connection connection1 = null;
        PreparedStatement preparedStatement = null;

        try {
            connection1 = sqlSessionFactory.openSession().getConnection();
            preparedStatement = connection1.prepareStatement(stringBuffer.toString());
            preparedStatement.executeUpdate();
        }finally {
            try {
                preparedStatement.close();
            }catch (Exception ignored){}
            try {
                connection1.close();
            }catch (Exception ignored){}
        }
    }


    public static class UserEntity {
        private String c_userid;
        private String c_code;
        private String c_name;
        private String c_sex;
        private String c_idcard;
        private String c_part_code;
        private String c_part_oid;
    }




}
